{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Variable Inspector Widget"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A short example implementation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook demonstrates how one can use the widgets already built in to IPython to create a working variable inspector much like the ones seen in popular commercial scientific computing environments."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipywidgets as widgets # Loads the Widget framework.\n",
    "from IPython.core.magics.namespace import NamespaceMagics # Used to query namespace.\n",
    "\n",
    "# For this example, hide these names, just to avoid polluting the namespace further\n",
    "get_ipython().user_ns_hidden['widgets'] = widgets\n",
    "get_ipython().user_ns_hidden['NamespaceMagics'] = NamespaceMagics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class VariableInspectorWindow(object):\n",
    "    instance = None\n",
    "    \n",
    "    def __init__(self, ipython):\n",
    "        \"\"\"Public constructor.\"\"\"\n",
    "        if VariableInspectorWindow.instance is not None:\n",
    "            raise Exception(\"\"\"Only one instance of the Variable Inspector can exist at a \n",
    "                time.  Call close() on the active instance before creating a new instance.\n",
    "                If you have lost the handle to the active instance, you can re-obtain it\n",
    "                via `VariableInspectorWindow.instance`.\"\"\")\n",
    "        \n",
    "        VariableInspectorWindow.instance = self\n",
    "        self.closed = False\n",
    "        self.namespace = NamespaceMagics()\n",
    "        self.namespace.shell = ipython.kernel.shell\n",
    "        \n",
    "        self._box = widgets.Box()\n",
    "        self._box.layout.overflow = 'visible scroll'\n",
    "        self._table = widgets.HTML(value = 'Not hooked')\n",
    "        self._box.children = [self._table]\n",
    "        \n",
    "        self._ipython = ipython\n",
    "        self._ipython.events.register('post_run_cell', self._fill)\n",
    "        \n",
    "    def close(self):\n",
    "        \"\"\"Close and remove hooks.\"\"\"\n",
    "        if not self.closed:\n",
    "            self._ipython.events.unregister('post_run_cell', self._fill)\n",
    "            self._box.close()\n",
    "            self.closed = True\n",
    "            VariableInspectorWindow.instance = None\n",
    "\n",
    "    def _fill(self):\n",
    "        \"\"\"Fill self with variable information.\"\"\"\n",
    "        values = self.namespace.who_ls()\n",
    "        self._table.value = '<div class=\"rendered_html jp-RenderedHTMLCommon\"><table><thead><tr><th>Name</th><th>Type</th><th>Value</th></tr></thead><tr><td>' + \\\n",
    "            '</td></tr><tr><td>'.join(['{0}</td><td>{1}</td><td>{2}'.format(v, type(eval(v)).__name__, str(eval(v))) for v in values]) + \\\n",
    "            '</td></tr></table></div>'\n",
    "\n",
    "    def _repr_mimebundle_(self, **kwargs):\n",
    "        return self._box._repr_mimebundle_(**kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "inspector = VariableInspectorWindow(get_ipython())\n",
    "inspector"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = 3.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c = a * b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = \"String\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "del b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "inspector.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
